package com.lingdong.onlinejudge.controller;

import com.lingdong.onlinejudge.annotation.AuthCheck;
import com.lingdong.onlinejudge.common.BaseResponse;
import com.lingdong.onlinejudge.common.ErrorCode;
import com.lingdong.onlinejudge.common.ResultUtils;
import com.lingdong.onlinejudge.exception.BusinessException;
import com.lingdong.onlinejudge.model.dto.submit.SubmitDto;
import com.lingdong.onlinejudge.model.dto.submit.SubmitQueryDto;
import com.lingdong.onlinejudge.model.dto.submit.SubmitUpdateExpDto;
import com.lingdong.onlinejudge.model.entity.Submit;
import com.lingdong.onlinejudge.model.vo.user.UserVo;
import com.lingdong.onlinejudge.service.SubmitService;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 提交
 * @author lzw
 */
@RestController
@RequestMapping("/submit")
public class SubmitController {

    @Resource
    private SubmitService submitService;

    /**
     * 题目提交
     * @param submitDto 提交信息
     * @return 结果
     */
    @PostMapping("/submit")
    public BaseResponse<Boolean> submit(@RequestBody SubmitDto submitDto,
                                        @RequestHeader("token") String token ) {
        if (submitDto == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = submitService.submit(submitDto, token);
        return ResultUtils.success(result);
    }


    /**
     * 根据比赛id 获取 当前登录角色所有的提交
     * @param matchId 比赛id
     * @param token 用户token
     *
     * @return 提交列表
     */

    @GetMapping("/getListByMatchId")
    public BaseResponse<List<Submit>> getAllSubmitByMatchId(Long matchId, @RequestHeader("token") String token){

        submitService.queryAllSubmit(matchId,token);
        return ResultUtils.success(new ArrayList<>());
    }


    /**
     * 根据提交id 添加分值
     * @param updateDto id 分值
     * @return 是否成功
     *
     */
    @AuthCheck(mustRole = "admin")
    @PutMapping("/updateSubmitExp")
    public BaseResponse<Boolean> updateSubmitExp(@RequestBody SubmitUpdateExpDto updateDto){

        return ResultUtils.success(submitService.updateSubmitExp(updateDto));
    }



    /**
     * 根据比赛id 结算分值(按照人来分组)
     * @param matchId 比赛id
     */
    @AuthCheck(mustRole = "admin")
    @GetMapping("/updateAllUserExpToSign")
    public BaseResponse<Boolean> updateAllUserExpToSign(@RequestParam Long matchId){
        return ResultUtils.success(submitService.updateAllUserExpToSign(matchId));
    }



    /**
     * 查找试题提交记录
     * @param queryDto 比赛id 用户id 试题id
     */
    @PostMapping("/selectOneSubmit")
    public BaseResponse<Submit> selectOneSubmit(@RequestBody SubmitQueryDto queryDto){
        return ResultUtils.success(submitService.selectOneSubmit(queryDto));
    }


    /**
     * 根据试题id,比赛id 查找提交列表
     * @param topicId 试题id
     * @param matchId 比赛id
     * @return 提交列表
     */
    @GetMapping("/submitList")
    public BaseResponse<List<Submit>> getSubmitList(Long topicId,Long matchId){

        return ResultUtils.success(submitService.getSubmitList(topicId,matchId));
    }



}
